//
//  ECMineSafeChangePasswordVc.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/15.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#define Textfield_height        960*0.1406         //封装输入框的高度


#import "ECMineSafeChangePasswordVc.h"
#import "ECUITextField_Account.h"
#import "ECRegularExpression.h"

@interface ECMineSafeChangePasswordVc (){
    
    ECUITextField_Account *uitf_oldPassword;
    ECUITextField_Account *uitf_newPassword;
    ECUITextField_Account *uitf_makeSurePassword;
    ECUButton *sureBtn;
}

@end

@implementation ECMineSafeChangePasswordVc

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改登录密码";
    
    uitf_oldPassword = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60), kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) , kProportion_Height(Textfield_height)) placeholder:@"原登录密码" leftImage:[UIImage imageNamed:@"pwd_user"]];
    [self.view addSubview:uitf_oldPassword];
    uitf_oldPassword.textField.secureTextEntry = YES;
    
    uitf_newPassword = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60) + CGRectGetMaxY(uitf_oldPassword.frame), kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) , kProportion_Height(Textfield_height)) placeholder:@"新密码(6-20位英文/数字/符号)" leftImage:[UIImage imageNamed:@"pwd_user"]];
    [self.view addSubview:uitf_newPassword];
    uitf_newPassword.textField.secureTextEntry = YES;
    
    uitf_makeSurePassword = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60) + CGRectGetMaxY(uitf_newPassword.frame), kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) , kProportion_Height(Textfield_height)) placeholder:@"确认新密码" leftImage:[UIImage imageNamed:@"pwd_user"]];
    [self.view addSubview:uitf_makeSurePassword];
    uitf_makeSurePassword.textField.secureTextEntry = YES;
    
    // login btn
    sureBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    sureBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(uitf_makeSurePassword.frame) + kProportion_Height(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
    [sureBtn addTarget:self action:@selector(action_Login) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorSelected"] forState:UIControlStateHighlighted];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:ceil(20)];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:sureBtn];
}

-(void)action_Login{
    
    if ([uitf_oldPassword.textField.text isEqualToString:@""] && [uitf_newPassword.textField.text isEqualToString:@""] && [uitf_makeSurePassword.textField.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入原登录密码"];
        return;
    }
    
    if (![uitf_oldPassword.textField.text isEqualToString:@""] && [ECRegularExpression detectionIsPasswordQualified:uitf_newPassword.textField.text] == NO) {
        [MBProgressHUD showTipMessageInWindow:kAlertTipOfPasswordValid];
        return;
    }
    
    if ([uitf_makeSurePassword.textField.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入确认新密码"];
        return;
    }
    
    if (![uitf_newPassword.textField.text isEqualToString:uitf_makeSurePassword.textField.text]) {
        [MBProgressHUD showTipMessageInWindow:@"两次密码不一致"];
        return;
    }
    
    [self getDataWithURL];
}

/*  请求数据  */
-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_UpdatePassword
                           params:@{@"currentPassword":uitf_oldPassword.textField.text,
                                    @"newPassword":uitf_newPassword.textField.text
                                    }
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              DLog(@"%@",json);
                              if ([json[@"code"] intValue] == 200) {
                                  [MBProgressHUD showTipMessageInWindow:@"密码修改成功"];
                                  [self.navigationController popViewControllerAnimated:YES];
                              }else{
                                  [MBProgressHUD showTipMessageInWindow:json[@"message"]];
                              }
                          }
                          failure:^(NSError *error) {
                        }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
